package com.antler.smt.portal.controller;

import com.aliyuncs.exceptions.ClientException;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.annotation.OperateCut;
import com.antler.smt.request.portal.*;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.MemberService;
import com.antler.smt.validate.DataValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  TODO
 *
 *  @author vic
 *  @date 2018-03-22 19:01
 *  
 */
@RestController
@RequestMapping(value = "/member")
@Api(description = "会员接口")
public class MemberController extends BaseController {


    @Autowired
    private MemberService memberService;

    /**
     * 会员注册
     *
     * @param request
     * @return
     */
    @PostMapping("/memberRegister")
    @ApiOperation(value = "会员注册", httpMethod = "POST")
    @OperateCut(operationContent = "会员注册")
    public Result memberRegister(@RequestBody MemberRegisterReq request) {
        DataValidator.isNull(request, "会员注册接口，请求参数不能为空！");
        DataValidator.isBlank(request.getVerificationCode(), "会员注册接口，验证码不能为空！");
        DataValidator.isBlank(request.getPassword(), "会员注册接口，密码不能为空！");
        DataValidator.isNull(request.getRegisterType(), "会员注册接口，注册类型不能为空！");
        DataValidator.isNull(request.getMemberType(), "会员注册接口，会员类型不能为空！");
        if (request.getRegisterType() != 1 && request.getRegisterType() != 2) {
            Result result = new Result(ResultCodeEnum.REQUEST_PARAM_ERROR);
            result.setMsg("注册类型不合法");
            return result;
        }
        if (request.getMemberType() != 1
                && request.getMemberType() != 2
                && request.getMemberType() != 3
                && request.getMemberType() != 4
                && request.getMemberType() != 5){
            Result result = new Result(ResultCodeEnum.REQUEST_PARAM_ERROR);
            result.setMsg("会员类型不合法");
            return result;
        }
        return memberService.memberRegister(request);
    }

    /**
     * 会员登录
     *
     * @param request
     * @return
     */
    @PostMapping("/memberLogin")
    @ApiOperation(value = "会员登录", httpMethod = "POST")
    @OperateCut(operationContent = "会员登录")
    public Result memberLogin(@RequestBody MemberLoginReq request) {

        DataValidator.isNull(request, "会员登录接口，请求参数不能为空！");
        DataValidator.isBlank(request.getUserName(), "会员登录接口，用户名不能为空！");
        DataValidator.isBlank(request.getPassword(), "会员登录接口，密码不能为空！");
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        return memberService.memberLogin(request, response);
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/memberLogout")
    @ApiOperation(value = "会员退出登录", httpMethod = "POST")
    @OperateCut(operationContent = "会员退出登录")
    public Result memberLogout() {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        HttpServletRequest request1 = requestAttributes.getRequest();
        Cookie[] cookies = request1.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("token")) {
                    cookie = new Cookie("token", null);
                    cookie.setMaxAge(0);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    break;
                }
            }
        }
        Result result = new Result();
        return result;
    }

    /**
     * 检验验证码
     *
     * @param request
     * @return
     */
    @PostMapping("/checkVerCode")
    @ApiOperation(value = "找回密码时校验验证码", httpMethod = "POST")
    public Result checkVerCode(@RequestBody CheckVerCodeReq request) throws ClientException {

        DataValidator.isNull(request, "校验验证码接口，请求参数不能为空！");
        DataValidator.isBlank(request.getVerCode(), "校验验证码接口，验证码不能为空！");
        DataValidator.isNull(request.getMemberId(), "校验验证码接口，会员id不能为空！");
        return memberService.checkVerCode(request);

    }

    /**
     * 新注册发送验证码
     *
     * @param request
     * @return
     * @throws ClientException
     */
    @PostMapping("/sendVerCodeForRegister")
    @ApiOperation(value = "新注册发送验证码", httpMethod = "POST")
    public Result sendVerCodeForRegister(@RequestBody SendVerCodeForFindPasswordReq request) throws ClientException {

        DataValidator.isNull(request, "发送验证码接口，请求参数不能为空！");
        DataValidator.isNull(request.getMethod(), "发送验证码接口，发送方式不能为空！");
        if (request.getMethod() != 1 && request.getMethod() != 2) {
            Result result = new Result(ResultCodeEnum.REQUEST_PARAM_ERROR);
            result.setMsg("发送方式不合法");
            return result;
        }
        return memberService.sendVerCodeForRegister(request);

    }

    /**
     * 发送验证码
     *
     * @param request
     * @return
     * @throws ClientException
     */
    @PostMapping("/sendVerCodeForFindPassword")
    @ApiOperation(value = "找回密码发送验证码", httpMethod = "POST")
    public Result sendVerCodeForFindPassword(@RequestBody SendVerCodeForFindPasswordReq request) throws ClientException {

        DataValidator.isNull(request, "发送验证码接口，请求参数不能为空！");
        DataValidator.isNull(request.getMethod(), "发送验证码接口，发送方式不能为空！");
        if (request.getMethod() != 1 && request.getMethod() != 2) {
            Result result = new Result(ResultCodeEnum.REQUEST_PARAM_ERROR);
            result.setMsg("发送方式不合法");
            return result;
        }
        return memberService.sendVerCodeForFindPassword(request);

    }


    /**
     * 重置密码
     *
     * @param request
     * @return
     */
    @PostMapping("/passwordReset")
    @ApiOperation(value = "重置密码", httpMethod = "POST")
    @OperateCut(operationContent = "重置密码")
    public Result passwordReset(@RequestBody ResetPasswordReq request) {

        DataValidator.isNull(request, "重置密码接口，请求参数不能为空！");
        DataValidator.isNull(request.getPassword(), "重置接口，密码不能为空！");
        DataValidator.isNull(request.getMemberId(), "重置接口，用户Id不能为空！");
        return memberService.resetPassword(request);
    }

}
